package com.qqtop10.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qqtop10.manager.model.FooterInfo;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.FooterInfoService;
import com.qqtop10.manager.utils.DateUtils;
import com.qqtop10.manager.utils.RandomUtils;

@Controller
@RequestMapping("/manager/footer/")
public class FooterInfoController extends BaseController {
	@Autowired
	@Qualifier("footerInfoService")
	private FooterInfoService footerInfoService;

	// 获取需要修改的文章详细内容
	@RequestMapping("footerTemp.do")
	public ModelAndView footerTemp(HttpServletRequest request, HttpServletResponse response, FooterInfo input,
			ModelAndView modelAndView) {
		FooterInfo footerInfo = footerInfoService.getInfoDetailed(input);
		modelAndView.setViewName("manager/footerTemp");
		modelAndView.addObject("info", footerInfo);
		return modelAndView;
	}

	@RequestMapping("insertInfo.do")
	@ResponseBody
	public Result insertInfo(HttpServletRequest request, HttpServletResponse response, FooterInfo footerInfo) {
		int id = footerInfo.getId();
		if (id > 0) {
			footerInfoService.updateInfoDetailed(footerInfo);
		} else {
			// 填充当前文章的一些必要信息
			footerInfo.setUuid(RandomUtils.makeUUID());
			String creator = getLogindName();
			footerInfo.setCreator(creator);
			footerInfo.setCreatetime(DateUtils.date());
			// 再插入当前文章作为显示文章
			footerInfoService.insertInfo(footerInfo);
		}
		return ok("操作成功");
	}

	@ResponseBody
	@RequestMapping("getFooterInfoList.do")
	public Result getFooterInfoList() {
		List<FooterInfo> footList = footerInfoService.getShowInfo();
		for (FooterInfo footerInfo : footList) {
			String text = footerInfo.getText();
			if (null != text && !"".equals(text)) {
				String regex = "(?i)(?s)<img[^>]*[/]?>";
				text = text.replaceAll(regex, "");
				if (text.length() > 30) {
					text = text.substring(0, 30);
					text += "......";
				}
				footerInfo.setText(text);
			}
		}
		Result result = new Result();
		result.setCode(1);
		result.setData(footList);
		result.setMsg("操作成功");
		return result;
	}

	@RequestMapping("updateInfo.do")
	@ResponseBody
	public Result updateInfo(HttpServletRequest request, HttpServletResponse response, FooterInfo footerInfo) {
		footerInfoService.updateInfoDetailed(footerInfo);
		Result result = new Result();
		result.setCode(1);
		result.setMsg("操作成功");
		return result;
	}

	@RequestMapping("deleteInfo.do")
	@ResponseBody
	public Result deleteInfo(int[] ids) {
		Result result = new Result();
		result.setCode(99);
		result.setMsg("删除失败");
		int count = 0;
		for (int i = 0; i < ids.length; i++) {
			// 如果为true 则为删除成功
			if (!footerInfoService.deleteInfo(ids[i])) {
				++count;
			}
		}

		if (count == 0) {
			result.setCode(1);
			result.setMsg("操作成功！");
			return result;
		} else if (count < ids.length) {
			result.setCode(99);
			result.setMsg("操作已完成。但有些数据没有删除成功。");
			return result;
		} else {
			result.setCode(99);
			result.setMsg("全部操作失败！");
			return result;
		}
	}
}
